package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.one.bo.IndicatorBo;
import com.one.dto.*;
import com.one.entity.*;
import com.one.enums.IsDeletedEnum;
import com.one.enums.IsLeafNode;
import com.one.enums.SexEnum;
import com.one.enums.StatusEnum;
import com.one.mapper.IndicatorInfoMapper;
import com.one.mapper.IndicatorOptionsMapper;
import com.one.mapper.SysMenuMapper;
import com.one.service.IndicatorInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.utils.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 指标表 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-10-09
 */
@Service
public class IndicatorInfoServiceImpl extends ServiceImpl<IndicatorInfoMapper, IndicatorInfoEntity> implements IndicatorInfoService {
    private final IndicatorInfoMapper indicatorInfoMapper;
    private final IndicatorOptionsMapper indicatorOptionsMapper;
    public IndicatorInfoServiceImpl(IndicatorInfoMapper indicatorInfoMapper,IndicatorOptionsMapper indicatorOptionsMapper) {
        this.indicatorInfoMapper = indicatorInfoMapper;
        this.indicatorOptionsMapper = indicatorOptionsMapper;
    }

    @Override
    public Result selectIndicator(IndicatorBo indicatorBo) {
        SelectIndicator selectIndicator = new SelectIndicator();
        selectIndicator.setName(indicatorBo.getName());
        selectIndicator.setId(indicatorBo.getId());
        //查询项目组评分 mybatis
        List<IndicatorInfo> result = indicatorInfoMapper.selectIndicator(selectIndicator);
        if (result.size() == 0) return Result.error().message("查询结果为空");
        if(result.size()==1){
            if(selectIndicator.getId()!=null && !selectIndicator.getId().equals("") ){
                return Result.ok().data(result);
            }
            selectIndicator.setId(null);
            selectIndicator.setName(null);
            selectIndicator.setParentIds(result.get(0).getParentIds());
            selectIndicator.setParentId(result.get(0).getId());
            result = indicatorInfoMapper.selectIndicator(selectIndicator);
        }
        return Result.ok().data(TreeUtils.createIndicatorTree(result,"0",100));
    }

    @Override
    public Result deleteIndicator(IndicatorBo indicatorBo) {
        DataCheckUtils.isNotBlank(indicatorBo.getId(), "指标id不能为空!");
        QueryWrapper<IndicatorInfoEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(IndicatorInfoEntity::getId, indicatorBo.getId()).eq(IndicatorInfoEntity::getIsDeleted, IsDeletedEnum.NO.getKey());;
        IndicatorInfoEntity indicatorInfoEntity = indicatorInfoMapper.selectOne(sectionQueryWrapper);
        //查询结果校验
        if (indicatorInfoEntity == null) {
            return Result.error().message("指标不存在！");
        }

        IndicatorInfo indicatorInfo = new IndicatorInfo();
        indicatorInfo.setParentId(indicatorInfoEntity.getId());
        //查询项目组评分 mybatis
        int row = indicatorInfoMapper.deleteIndicator(indicatorInfo);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除指标失败");
        }
        return Result.ok();
    }

    @Override
    public Result addIndicator(IndicatorInfoEntity indicatorInfoEntity) {
        indicatorInfoEntity.setIsDeleted("0");
        indicatorInfoEntity.setCreateBy("1");
        indicatorInfoEntity.setCreateTime(DateUtils.getCurrentTime());
        //判断插入结果
        if (SqlHelper.retBool(indicatorInfoMapper.insert(indicatorInfoEntity))==false) {
            return Result.error().message("添加菜单失败");
        }
        IndicatorInfoEntity indicatorInfoEntity1 = new IndicatorInfoEntity();
        indicatorInfoEntity1.setParentIds(indicatorInfoEntity.getParentIds()+","+indicatorInfoEntity.getId());
        indicatorInfoEntity1.setCode(indicatorInfoEntity.getId());
        UpdateWrapper<IndicatorInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(IndicatorInfoEntity::getId, indicatorInfoEntity.getId());
        int row = indicatorInfoMapper.update(indicatorInfoEntity1, updateWrapper);
        //判断插入结果
        if (row == 0) {
            return Result.error().message("更新菜单失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateIndicator(IndicatorInfoEntity indicatorInfoEntity) {
        UpdateWrapper<IndicatorInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(IndicatorInfoEntity::getId, indicatorInfoEntity.getId());
        int row = indicatorInfoMapper.update(indicatorInfoEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑菜单信息失败");
        }
        return Result.ok();
    }

    @Override
    public Result selectIndicatorOptions(IndicatorBo indicatorBo) {
        QueryWrapper<IndicatorOptionsEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(IndicatorOptionsEntity::getIndCode, indicatorBo.getId()).eq(IndicatorOptionsEntity::getIsDeleted, IsDeletedEnum.NO.getKey()).orderByAsc(IndicatorOptionsEntity::getSort);
        List<IndicatorOptionsEntity> indicatorOptionsEntities = indicatorOptionsMapper.selectList(sectionQueryWrapper);
        if (indicatorOptionsEntities.size() == 0) {
            return Result.error().message("查询结果为空");
        }
        return Result.ok().data(indicatorOptionsEntities);
    }

    @Override
    public Result addIndicatorOptions(IndicatorOptionsEntity indicatorOptionsEntity) {
        indicatorOptionsEntity.setIsDeleted("0");
        indicatorOptionsEntity.setCreateBy("1");
        indicatorOptionsEntity.setCreateTime(DateUtils.getCurrentTime());
        int row = indicatorOptionsMapper.insert(indicatorOptionsEntity);
        //判断插入结果
        if (row == 0) {
            return Result.error().message("新增指标失败");
        }
        return Result.ok();
    }

    @Override
    public Result deleteIndicatorOptions(IndicatorBo indicatorBo) {
        DataCheckUtils.isNotBlank(indicatorBo.getIndicatorOptionsId(), "指标id不能为空!");
        IndicatorOptionsEntity indicatorOptionsEntity = new IndicatorOptionsEntity();
        indicatorOptionsEntity.setIsDeleted(IsDeletedEnum.YES.getKey());
        UpdateWrapper<IndicatorOptionsEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(IndicatorOptionsEntity::getId, indicatorBo.getIndicatorOptionsId()).eq(IndicatorOptionsEntity::getIsDeleted,IsDeletedEnum.NO.getKey());
        int row = indicatorOptionsMapper.update(indicatorOptionsEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateIndicatorOptions(IndicatorOptionsEntity indicatorOptionsEntity) {
        UpdateWrapper<IndicatorOptionsEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(IndicatorOptionsEntity::getId, indicatorOptionsEntity.getId());
        int row = indicatorOptionsMapper.update(indicatorOptionsEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑信息失败");
        }
        return Result.ok();
    }

    @Override
    public Result getIndicators() {
        QueryWrapper<IndicatorInfoEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(IndicatorInfoEntity::getIsDeleted, IsDeletedEnum.NO.getKey()).eq(IndicatorInfoEntity::getParentId, '0');
        List<IndicatorInfoEntity> indicatorInfoEntities = indicatorInfoMapper.selectList(sectionQueryWrapper);
        if (indicatorInfoEntities.size() == 0) {
            return Result.error().message("没有有效角色");
        }
        List<EnumResult> result = indicatorInfoEntities.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getName())).collect(Collectors.toList());
        return Result.ok().data(result);
    }

    @Override
    public Result selectQuestionnaireIndicatorOptions(IndicatorBo indicatorBo) {
        SelectIndicator selectIndicator = new SelectIndicator();
        selectIndicator.setId(indicatorBo.getId());
        //查询项目组评分 mybatis
        List<IndicatorInfo> result = indicatorInfoMapper.selectIndicator(selectIndicator);
        if (result.size() == 0) return Result.error().message("查询结果为空");
        if(result.size()==1){
            selectIndicator.setId(null);
            selectIndicator.setName(null);
            selectIndicator.setParentIds(result.get(0).getParentIds());
            selectIndicator.setParentId(result.get(0).getId());
            result = indicatorInfoMapper.selectIndicator(selectIndicator);
        }
        for(IndicatorInfo indicatorInfo:result){
            if(indicatorInfo.getIsLeafNode().equals(IsLeafNode.YES.getKey())){
                IndicatorBo indicatorBo1 =  new IndicatorBo();
                indicatorBo1.setId(indicatorInfo.getId());
                indicatorInfo.setOptions((List<IndicatorOptionsEntity>) selectIndicatorOptions(indicatorBo1).getData());
            }
        }
        List<IndicatorInfo> questionnaires = TreeUtils.createIndicatorTree(result,"0",100);
        return Result.ok().data(questionnaires);
    }
}
